<template>
    <el-dialog :title="title" :visible="visible" width="30%" :show-close="true">
        <el-form :model="ruleForm" :rules="rules" ref="formRef" label-width="120px" class="demo-ruleForm">
            <el-form-item label="分类名称" prop='name'>
                <el-input v-model="ruleForm.name" placeholder="请输入名称"></el-input>
            </el-form-item>
            <el-form-item label="分类图标" prop="icon">
                <Upload v-model="ruleForm.icon" />
            </el-form-item>
            <el-form-item label="分类排序" prop="sort">
                <el-input type="number" v-model="ruleForm.sort" placeholder="请输入地址" />
            </el-form-item>
            <el-form-item label="分类状态" prop="status">
                <el-select v-model="ruleForm.status" placeholder="请选择" style="width: 100%;">
                    <el-option label="启用" :value="1" />
                    <el-option label="禁用" :value="0" />
                </el-select>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="close">取 消</el-button>
            <el-button type="primary" @click="submit" :loading="loading">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>
import { merchantCategoryAdd, merchantCategoryEdit } from '../api/index';
import Upload from '@/components/Upload.vue';
export default {
    props: ['merchant_id'],
    data() {
        return {
            isEdit: false,
            options: [],
            title: '',
            visible: false,
            loading: false,
            ruleForm: {
                name: null,
                icon: null,
                sort: null,
                status: null,
            },
            rules: {
                name: [
                    { required: true, message: '请输入名称', trigger: 'blur' },
                    { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
                ],
                icon: [
                    { required: true, message: '请上传分类图标', trigger: 'blur' }
                ],
                // sort: [
                //     { required: true, message: '请输入分类排序', trigger: 'blur' }
                // ],
                status: [
                    { required: true, message: '请选择分类状态', trigger: 'blur' }
                ],
            },
        }
    },
    components: { Upload },
    created() {

    },
    methods: {
        add() {
            this.visible = true;
            this.isEdit = false;
            this.title = '新增商品分类';
            let temp = {};
            Object.keys(this.ruleForm).forEach(key => {
                temp[key] = null
            })
            this.ruleForm = temp;
        },
        edit(data) {
            this.title = '修改商品分类';
            this.visible = true;
            this.isEdit = true;
            this.editId = data.id;
            Object.keys(this.ruleForm).forEach(key => {
                this.ruleForm[key] = data[key]
            })
        },
        close() {
            this.$refs.formRef.resetFields();
            this.visible = false;
        },
        submit() {
            this.$refs.formRef.validate(async (res) => {
                if (res) {
                    this.loading = true;
                    try {
                        const p = { ...this.ruleForm }
                        let res = this.isEdit ? await merchantCategoryEdit({ ...p, id: this.editId }) : await merchantCategoryAdd(p);
                        this.loading = false;
                        this.$refs.formRef.resetFields();
                        const msg = this.title + '成功';
                        this.$message({
                            type: 'success',
                            message: msg
                        })
                        this.close();
                        this.$emit('refresh', 1);
                    } catch (err) {
                        console.log(err);

                        this.loading = false;
                    }
                }
            })
        },
    },
}
</script>

<style lang='scss' scoped>
.avatar-uploader .el-upload {
    border: 1px dashed red;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}

.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>